﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EventBus.Abstractions;
using GraphQLSample.Application.DomainEventHandlers;
using GraphQLSample.Domain.Core;
using GraphQLSample.Infrastructure.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace GraphQLSample.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DefaultController : ControllerBase
    {
        private readonly ApplicationContext _ctx;
        private readonly IEventBus _eventBus;
        public DefaultController(IEventBus eventBus,
             ApplicationContext ctx)
        {
            _eventBus = eventBus;
            _ctx = ctx;
        }
        public async Task CreateAsync()
        {
            await _eventBus.PublishAsync(new UserCreatedEvent("gainorloss"));
        }
        public async Task<List<User>> GetAllAsync()
        {
           var list= await _ctx.User.AsNoTracking()
                 .ToListAsync();
            return list;
        }
    }
}